
public class PitchTranslation {
	public static int translate(int pitch, int lastPitch, int lastPitchTr){
		int newPitch=pitch;
		int[] nearestPitches = {0,0,0};
		boolean endSearch=false;
		if (Main.notesArrayPointer>0){
			int pointer=Main.notesArrayPointer-1;
	
			while (pointer>0&&endSearch==false){
				if (Main.notesArray[pointer][0]%12==pitch%12){
					nearestPitches[1]=pointer;
					endSearch=true;
				}
				pointer=pointer-1;
			}
			
			if (pointer==1){
				nearestPitches[1]=0;
			}
	
			endSearch=false;
			pointer=Main.notesArrayPointer;
			while (pointer>0&&endSearch==false){
				if (Main.notesArray[pointer][0]%12==(pitch-1)%12){
					nearestPitches[0]=pointer;
					endSearch=true;
				}
				pointer=pointer-1;
			}
			if (pointer==1){
				nearestPitches[0]=0;
			}
			
			endSearch=false;
			pointer=Main.notesArray.length-1;
			while (pointer>0&&endSearch==false){
				if (Main.notesArray[pointer][0]%12==(pitch+1)%12){
					nearestPitches[2]=pointer;
					endSearch=true;
				}
				pointer=pointer-1;
			}
			if (pointer==1){
				nearestPitches[2]=0;
			}
	
	
			if (nearestPitches[0]>0&&nearestPitches[0]>nearestPitches[2]&&nearestPitches[0]>nearestPitches[1]+3){
				newPitch=pitch-1;
			}
			else if (nearestPitches[2]>=nearestPitches[0]&&nearestPitches[2]>nearestPitches[1]+3){
				newPitch=pitch+1;
			}
			else{
				newPitch=pitch;
			}
//			if(newPitch==lastPitchTr&&pitch!=lastPitch){
//				newPitch=lastPitchTr-(lastPitch-pitch);
//			}
			if (newPitch!=pitch){
//				System.out.println("Altered pitch!!");
			}
		}
		return newPitch;
	}
}